﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ReallyReallyReallySimpleRoguelike.Helpers;

namespace ReallyReallyReallySimpleRoguelike.Managers
{
    public class ActionManager
    {
        //Stores a list of actions along with the keys mapped to those actions
        //Checks action map against the inputmanager
        public ActionMap[] ActionMaps;
        InputManager InputManager = DungeonManager.Instance.InputManager;

        private void Initialize(int numberOfActions)
        {
            ActionMaps = new ActionMap[numberOfActions];
            for (int i = 0; i < numberOfActions; i++)
                ActionMaps[i] = new ActionMap();
        }

        //public bool IsActionDown(Actions Action) 
        //{
        //    for (int i = 0; i < ActionMaps[(int)Action].ConsoleKeys.Count(); i++) 
        //    {
        //        if (InputManager.KeyDown(ActionMaps[(int)Action].ConsoleKeys[i]))
        //        {
        //            return true;
        //        }
        //    }
        //    return false;
        //}

        public bool IsActionPushed(Actions Action)
        {
            for (int i = 0; i < ActionMaps[(int)Action].ConsoleKeys.Count(); i++)
            {
                if (InputManager.KeyPush(ActionMaps[(int)Action].ConsoleKeys[i]))
                {
                    return true;
                }
            }
            return false;
        }

        //public bool IsActionReleased(Actions Action)
        //{
        //    for (int i = 0; i < ActionMaps[(int)Action].ConsoleKeys.Count(); i++)
        //    {
        //        if (InputManager.KeyReleased(ActionMaps[(int)Action].ConsoleKeys[i]))
        //        {
        //            return true;
        //        }
        //    }
        //    return false;
        //}

       
        public void SetDefaultKeys()
        {
            Initialize(Enum.GetNames(typeof(Actions)).Length);
            SetButtons(Actions.North, ConsoleKey.UpArrow);
            SetButtons(Actions.South,  ConsoleKey.DownArrow);
            SetButtons(Actions.East,  ConsoleKey.RightArrow);
            SetButtons(Actions.West, ConsoleKey.LeftArrow);
            SetButtons(Actions.JumpNorth, ConsoleKey.W);
            SetButtons(Actions.JumpSouth, ConsoleKey.S);
            SetButtons(Actions.JumpEast, ConsoleKey.D);
            SetButtons(Actions.JumpWest, ConsoleKey.A);
            SetButtons(Actions.ShootNorth, ConsoleKey.U);
            SetButtons(Actions.ShootSouth, ConsoleKey.J);
            SetButtons(Actions.ShootEast, ConsoleKey.K);
            SetButtons(Actions.ShootWest, ConsoleKey.H);
            SetButtons(Actions.Wait, ConsoleKey.OemPeriod);
            SetButtons(Actions.Quit, ConsoleKey.Q);SetButtons(Actions.Quit, ConsoleKey.Escape);
            SetButtons(Actions.Help, ConsoleKey.F1);
            SetButtons(Actions.SoundToggle, ConsoleKey.F3);
             
        }

        #region SetButtons
        public void SetButtons(Actions Action, ConsoleKey Key)
        {
            ActionMaps[(int)Action].ConsoleKeys.Add(Key);
        }
        public void SetButtons(Actions Action, List<ConsoleKey> Keys)
        {
            ActionMaps[(int)Action].ConsoleKeys.AddRange(Keys);
        }
        #endregion

    }

    public enum Actions
    {
        North = 0,
        South,
        East,
        West,
        JumpNorth,
        JumpSouth,
        JumpEast,
        JumpWest,
        ShootNorth,
        ShootSouth,
        ShootEast,
        ShootWest,
        Pick,
        Drop,
        Wait,
        Quit,
        Help,
        SoundToggle
    }
}
